package com.llly.controller;


import com.llly.client.BackstageProductFeignClient;
import com.llly.pojo.Easybuy_product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class BackstageProductController {
    @Autowired
    private BackstageProductFeignClient backstageproductfeignclient;

    public ModelAndView Allss(ModelAndView mv, Integer PageIndex) {
        Map<String, Object> stringObjectMap = backstageproductfeignclient.Allproduct_category(PageIndex == null ? 1 : PageIndex);
        List<Map<String, Object>> result = (List<Map<String, Object>>) stringObjectMap.get("Result");
        mv.addObject("maps", result);
        mv.addObject("Total", stringObjectMap.get("Total"));
        mv.addObject("PageNum", stringObjectMap.get("PageNum"));
        mv.addObject("Pages", stringObjectMap.get("Pages"));
        mv.addObject("PageNo", stringObjectMap.get("PageNo"));
        Integer pages = (Integer) stringObjectMap.get("Pages");
        List pag = new ArrayList();
        for (int i = 1; i <= pages; i++) {
            pag.add(i);
        }
        mv.addObject("listPage", pag);
        return mv;
    }

    public ModelAndView  Allxx(ModelAndView mv,Integer PageIndex) {
        Map<String, Object> stringObjectMap = backstageproductfeignclient.Alleasybuy_product(PageIndex == null ? 1 : PageIndex);
        List<Easybuy_product> result = (List<Easybuy_product>) stringObjectMap.get("Result");
        mv.addObject("easybuy_products", result);
        mv.addObject("Total", stringObjectMap.get("Total"));
        mv.addObject("PageNum", stringObjectMap.get("PageNum"));
        mv.addObject("Pages", stringObjectMap.get("Pages"));
        mv.addObject("PageNo", stringObjectMap.get("PageNo"));
        Integer pages = (Integer) stringObjectMap.get("Pages");
        List pag = new ArrayList();
        for (int i = 1; i <= pages; i++) {
            pag.add(i);
        }
        mv.addObject("listPage", pag);
        return mv;
    }
    //管理页面，分类管理，查询
    @RequestMapping("/Allproductcategory")
    public ModelAndView Allproduct_category(ModelAndView mv,Integer PageIndex){
        Allss(mv,PageIndex);
        mv.setViewName("Manager/product");
        return mv;
    }
    //管理页面，分类管理，删除
    @RequestMapping("/deleteproductcategory")
    public ModelAndView deleteproductcategory(Integer id,ModelAndView mv,Integer PageIndex){

            int i = backstageproductfeignclient.ideasybuy_product(id);
            if(id == null){
                mv.addObject("messa",0); //分页
                i = 0;
            }
        Allss(mv,PageIndex);
        if(i==0){
            int deleteproductcategory = backstageproductfeignclient.deleteproductcategory(id);
            if(deleteproductcategory>0){

                mv.addObject("messa",1); //成功
                mv.setViewName("Manager/product");
            }else {
                mv.addObject("messa",2); //失败
                mv.setViewName("Manager/product");
            }
        }else {
            mv.addObject("messa",-1); //不能删
            mv.setViewName("Manager/product");
        }


                    return mv;
    }
    //管理员页面，分类管理，新增的回显
    @RequestMapping("/addproductcategoryss")
    public ModelAndView addproductcategoryss(ModelAndView mv,Integer PageIndex) {
        List<Map<String, Object>> maps = backstageproductfeignclient.Allproduct_category2();
        mv.addObject("maps", maps);
        mv.setViewName("Manager/productClass-add");
        return mv;
    }

    //管理员页面，分类管理，新增
    @RequestMapping("/addproductcategory")
    public ModelAndView addproductcategory(ModelAndView mv,String name,Integer type,Integer class1,Integer class2,Integer PageIndex, Integer parentId){
        System.out.println("class1:"+class1);
        System.out.println("class2:"+class2);
        if(type==1){
            parentId=0;
        }else if (type==2){
            parentId=class1;
        }else if(type==3){
            parentId=class2;
        }
        int addproductcategory = backstageproductfeignclient.addproductcategory(name,type,parentId);
            if (addproductcategory>0){
                Allss(mv,PageIndex);
                mv.setViewName("Manager/product");
            }else {
                mv.setViewName("Manager/product");
            }
        return mv;
    }
    //管理页面，商品管理，查询
    @RequestMapping("/Alleasybuy_product")
    public ModelAndView  Alleasybuy_product(ModelAndView mv,Integer PageIndex){
        Allxx(mv,PageIndex);
        mv.setViewName("Manager/product-modify");
        return mv;
    }
    //管理页面，商品管理，删除
    @RequestMapping("/deleteproduct")
    public ModelAndView deleteproduct(Integer id,ModelAndView mv,Integer PageIndex){
        int deleteproduct = backstageproductfeignclient.deleteproduct(id);
        if(deleteproduct>0){
            Allxx(mv,PageIndex);
            mv.setViewName("Manager/product-modify");
        }else {
            mv.setViewName("Manager/product-modify");
        }
        return mv;
    }
    //管理员页面,商品管理，id查询，修改的回显
    @RequestMapping("/idproduct")
    public ModelAndView idproduct(Integer id,ModelAndView mv){
        List<Map<String, Object>> maps = backstageproductfeignclient.Allproduct_category2();
        List<Easybuy_product> idproduct = backstageproductfeignclient.idproduct(id);
        mv.addObject("idproduct",idproduct);
        mv.addObject("idproduct2",maps);
        mv.setViewName("Manager/productClass-modify");
        return mv;
    }
    //管理员页面,商品管理，修改
    @RequestMapping("/updateproduct")
    public ModelAndView updateproduct(Easybuy_product ep, ModelAndView mv, Integer PageIndex, @RequestParam("photo")MultipartFile photo,String fileName){
        if (!photo.isEmpty()){
            String originalFilename =photo.getOriginalFilename();
            String s = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            ep.setFileName(s+originalFilename);
            //给实体类复制fileName 保存到数据库
            String path="D:\\EasyBuy\\easybuy-consumer-8081\\src\\main\\resources\\templates\\images";
            File file=new File(path,s+originalFilename);
            try {
                photo.transferTo(file);
                backstageproductfeignclient.updateproduct(ep);
                Allxx(mv,PageIndex);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            ep.setFileName(fileName);
            backstageproductfeignclient.updateproduct(ep);
        }
        mv.setViewName("forward:/Alleasybuy_product");
        return mv;
    }
}
